<?php
// +----------------------------------------------------------------------
// | KITEGO-Admin「开箱即用」「人人全栈」
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.kitego.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed KITEGO并不是自由软件，未经许可不能去掉KITEGO相关版权
// +----------------------------------------------------------------------
// | Author: KITEGO Team <bd@kitego.cn>
// +----------------------------------------------------------------------

namespace app\adminapi\controller\v1\maintenance;

use app\adminapi\services\maintenance\LangService;
use app\adminapi\validate\maintenance\LangValidate;
use Exception;
use kitego\base\BaseController;
use think\App;
use think\response\Json;

class LangController extends BaseController
{
    public array $safeAction = ['list'];

    private LangService $service;

    public function __construct(App $app, LangService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 语言列表
     * @throws Exception
     */
    public function list(): Json
    {
        $params = $this->params;
        $params['page'] = !empty($params['page']) ? $params['page'] : 1;
        $params['pageSize'] = !empty($params['pageSize']) ? $params['pageSize'] : 10;
        $params['keywords'] = !empty($params['keywords']) ? $params['keywords'] : null;
        $params['status'] = $params['status'] ?? null;

        return $this->service->list($params['page'], $params['pageSize'], $params['keywords'], $params['status']);
    }

    /**
     * 编辑语言
     * @throws Exception
     */
    public function edit(): Json
    {
        $params = $this->params;
        $params['id'] = $params['id'] ?? null;
        Validate(LangValidate::class)->scene('edit')->check($params);

        return $this->service->edit(
            $params['id'], $params['sort'], $params['langName'], $params['langFlag'],
            $params['status'], $params['isDelete'], $params['isDefault']
        );
    }

    /**
     * 批量删除
     */
    public function batchDel(): Json
    {
        $params = $this->params;
        if (!isset($params['ids']) || !is_array($params['ids'])) {
            exception('参数类型必须是数组');
        }

        return $this->service->batchDel($params['ids']);
    }
}